package com.mengchengnet.chaperonage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/17.
 */

public class PocketActivity extends BaseActivity  {
    private LinearLayout ll;
    private TextView money;
    private RequestQueue requestQueue;
    private String amount;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (amount.isEmpty()){
                money.setText("0.00");
            }else {
                money.setText(amount);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pocket);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("钱包");
        requestQueue= Volley.newRequestQueue(this);
        money= (TextView) findViewById(R.id.pocket_amount);

        ll= (LinearLayout) findViewById(R.id.pocket_cash);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //startActivity(CashActivity.class);
                Intent intent =new Intent(getApplicationContext(),CashActivity.class);
                startActivity(intent);
            }
        });
        if(!PreferenceUtil.getString(getApplication(),"phone").isEmpty()){
            getAmout();
        }

    }


    private void getAmout(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.getAmount,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("rrrrrrrrr", "获取钱包余额返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                amount=jsonObject.getString("amount");
                                handler.sendEmptyMessage(0);
                            }
                            else {
                                Toast.makeText(PocketActivity.this, "余额获取失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(getApplication(), "网络异常,请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("loginId", PreferenceUtil.getString(getApplication(),"phone"));
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }


    private void get(){
        StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.1.43/wap/index.php?ctl=abc&act=index",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("rrrrrrrrr", "返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                Toast.makeText(PocketActivity.this, "余额获取成功", Toast.LENGTH_SHORT).show();
                                JSONArray array=jsonObject.getJSONArray("cityList");


                            }
                            else {


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(getApplication(), "网络异常,请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("id", "123");
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
